package framework.base.ui;

import java.io.File;

import z.utils.LogUtils;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import framework.base.config.AppParameters;
import framework.base.config.PhoneParameters;
import framework.base.service.MainService;
import framework.cache.utils.Md5Utils;

public class FirstAct extends BaseAct {

	@Override
	protected void onCreateAct(Bundle savedInstanceState) {
		super.onCreateAct(savedInstanceState);
		setParam();
	}

	private void setParam() {
		// 获取全局context
		setContextGlobal();
		// 保存手机参数
		savePhoneArg();
		// 启动服务
		startService();
		// 应用参数获取
		appVersion();
		// 是否打印

	}

	private void setContextGlobal() {
		ContextManager.getInstance().setContext(getApplicationContext());
	}

	protected void setTrace(boolean isTrace) {
		LogUtils.IS_TRACE = isTrace;
	}

	protected void setRate(int width) {
		PhoneParameters.designWidth = width;
	}

	protected void setSdcard(String appName) {
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			// 存在sdcard
			File sdcardDir = Environment.getExternalStorageDirectory();
			// download目录
			PhoneParameters.downloadDir = sdcardDir.getAbsolutePath() + "/" + appName + "/download/";
			// 图片缓存
			PhoneParameters.imageFileDir = sdcardDir.getAbsolutePath() + "/" + appName + "/file/image/";
			// 声音缓存
			PhoneParameters.soundFileDir = sdcardDir.getAbsolutePath() + "/" + appName + "/file/sound/";
			// app数据缓存
			PhoneParameters.appDataDir = sdcardDir.getAbsolutePath() + "/" + appName + "/file/" + Md5Utils.MD5("app") + "/";
		}
	}

	// protected void startNextAct(final Class clazz) {
	// final Timer timer = new Timer();
	// timer.schedule(new TimerTask() {
	// @Override
	// public void run() {
	// timer.cancel();
	// openAct(clazz);
	// animationNext();
	// }
	//
	// }, 1000);
	// }
	//
	// protected void animationNext() {
	// }

	private void savePhoneArg() {
		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);
		// 手机分辨率
		PhoneParameters.screenWidth = metric.widthPixels; // 屏幕宽度（像素）
		PhoneParameters.screenHight = metric.heightPixels; // 屏幕宽度（像素）
		PhoneParameters.density = metric.density; // 屏幕密度（0.75 / 1.0 / 1.5）
		// 手机品牌
		PhoneParameters.manufacturer = Build.MANUFACTURER;
		// 手机型号
		PhoneParameters.model = Build.MODEL;
		// 手机系统版本号
		PhoneParameters.release = Build.VERSION.RELEASE;
		PhoneParameters.sdk_version = android.os.Build.VERSION.SDK_INT;
		// token
		PhoneParameters.token = getPhoneToken();
		// download目录
		PhoneParameters.downloadDir = getFilesDir().getAbsolutePath() + "/download/";
		// 图片缓存
		PhoneParameters.imageFileDir = getFilesDir().getAbsolutePath() + "/image/";
		// 声音缓存
		PhoneParameters.soundFileDir = getFilesDir().getAbsolutePath() + "/sound/";
		// app数据缓存
		PhoneParameters.appDataDir = getCacheDir().getAbsolutePath() + "/" + Md5Utils.MD5("app") + "/";
	}

	private String getPhoneToken() {
		return "";
	}

	private void startService() {
		if (!MainService.serviceState) {
			Intent intent = new Intent(this, MainService.class);
			startService(intent);
		}
	}

	private void appVersion() {
		PackageInfo info = null;
		try {
			info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
		} catch (NameNotFoundException e) {
			exception(e);
		}
		AppParameters.VERSION_CODE = info.versionCode;
		AppParameters.VERSION_NAME = info.versionName;
	}
}
